using DimensionsHelper.Common.Utils;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

public class AnalysisQualifiedName : AnalysisNode
{
    private readonly AnalysisQualifiedNameFlags _flags;
    private readonly AnalysisIdentifier[] _names;

    internal AnalysisQualifiedName(QualifiedName syntaxNode, AnalysisNode? parent, AnalysisQualifiedNameFlags flags) :
        base(AnalysisBoundKinds.QualifiedName, syntaxNode, parent)
    {
        Syntax = syntaxNode;
        _names = new AnalysisIdentifier[syntaxNode.Count];
        _flags = flags;
        syntaxNode.ForEach((n, i) => _names[i] = AnalysisNodeFactory.Identifier(n, this, null));
    }

    public override QualifiedName Syntax { get; }

    public override bool IsExpression => false;

    public override bool IsStatement => false;

    public override bool IsMetadata => false;

    public bool IsModuleName => (_flags & AnalysisQualifiedNameFlags.ModuleName) != 0;

    public bool IsInheritInterfaceName => (_flags & AnalysisQualifiedNameFlags.InheritInterfaceName) != 0;

    public bool IsInheritClassName => (_flags & AnalysisQualifiedNameFlags.InheritClassName) != 0;

    public bool IsImplementInterfaceName => (_flags & AnalysisQualifiedNameFlags.ImplementInterfaceName) != 0;

    public override int ChildrenCount => _names.Length;


    internal override void AddChild(AnalysisNode child)
    {
        throw new NotSupportedException("AnalysisDeclaration is read-only.");
    }

    public override IEnumerable<AnalysisNode> GetChildren()
    {
        return _names;
    }
}


public static partial class AnalysisNodeFactory
{
    /// <summary>
    /// 创建一个全限定名
    /// </summary>
    /// <param name="syntaxNode">原始语法节点</param>
    /// <param name="parent">父节点</param>
    /// <param name="flags">节点标签</param>
    public static AnalysisQualifiedName QualifiedName(QualifiedName syntaxNode, AnalysisNode? parent, 
        AnalysisQualifiedNameFlags flags = AnalysisQualifiedNameFlags.BindTypeName)
    {
        return new AnalysisQualifiedName(syntaxNode, parent, flags);
    }
}
